package com.lys.emsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.emsystem.entity.Admin;
import com.lys.emsystem.entity.Job;
import com.lys.emsystem.entity.Teacher;
import com.lys.emsystem.service.IAdminService;
import com.lys.emsystem.service.IJobService;
import com.lys.emsystem.service.ITeachService;
import com.lys.emsystem.service.impl.TeacherServiceImpl;
import com.lys.emsystem.utils.JWTUtils;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.JobVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liyansheng
 * @since 2022-04-30
 */
@RestController
@CrossOrigin
@RequestMapping("/job")
public class JobController {

    @Autowired
    private IJobService jobService;

    @Autowired
    private TeacherServiceImpl teacherService;

    @Autowired
    private IAdminService adminService;

    @PostMapping
    public Result search(@RequestBody JobVo jobVo) {
        IPage<Job> pages = jobService.search(new Page<>(jobVo.getPageIndex(), jobVo.getPageSize()), jobVo);
        if (pages.getSize() > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("list", pages.getRecords());
            map.put("totalCount", pages.getTotal());
            return Result.buildResult(Result.Status.OK, map);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Job job, @RequestHeader("token") String token) {
        System.out.println(job);
        //获取用户类型
        Integer userType = JWTUtils.getUserType(token);
        //获取用户的id
        Integer userId = JWTUtils.getUserId(token);
        //判断用户类型
        if (userType == 2) {
        //    教师
        //    获取教师姓名
            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
            wrapper.eq("id", userId);
            Teacher teacher = teacherService.getOne(wrapper);
            job.setPublisher(teacher.getName());
            job.setStatus(false);
        }
        if (userType == 3) {
        //    管理员
            QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", userId);
            Admin admin = adminService.getOne(queryWrapper);
            job.setPublisher(admin.getName());
        }
        //获取时间
        job.setCreateTime(LocalDateTime.now());
        if (jobService.save(job)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Job job) {
        System.out.println(job);
        if (jobService.updateById(job)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Job job) {
        if (jobService.removeById(job.getId())) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delList")
    public Result delList(@RequestBody JobVo jobVo) {
        if (jobService.removeByIds(Arrays.asList(jobVo.getIds()))) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/find")
    public Result find(@RequestBody Job job) {
        List<Job> jobList=jobService.find(job);
        if (jobList.size() > 0) {
            return Result.buildResult(Result.Status.OK,jobList);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }
}
